package com.open.capacity.prints.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.prints.dao.BillGhxjckptDao;
import com.open.capacity.prints.entity.BillGhxjckpt;
import com.open.capacity.prints.service.BillGhxjckptService;
import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class BillGhxjckptServiceImpl implements BillGhxjckptService {

    @Autowired
    private BillGhxjckptDao billGhxjckptDao;

    /**
     * 添加
     *
     * @param billGhxjckpt
     */
    public int save(BillGhxjckpt billGhxjckpt) {
        billGhxjckpt.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billGhxjckpt.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhxjckpt.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据

        billGhxjckpt.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhxjckpt.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhxjckpt.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhxjckpt.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhxjckpt.getMoney2()));
        return billGhxjckptDao.save(billGhxjckpt);
    }

    /**
     * 修改
     *
     * @param billGhxjckpt
     */
    public int update(BillGhxjckpt billGhxjckpt) {
        billGhxjckpt.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhxjckpt.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据

        billGhxjckpt.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhxjckpt.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhxjckpt.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhxjckpt.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhxjckpt.getMoney2()));
        return billGhxjckptDao.update(billGhxjckpt);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billGhxjckptDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillGhxjckpt findById(String id) {
        BillGhxjckpt item = billGhxjckptDao.findById(id);
        BigDecimal resut = item.getMoney2();
        Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
        item.setMillion5(million5Count);
        resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

        Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
        item.setMillion4(million4Count);
        resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


        Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
        item.setMillion3(million3Count);
        resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

        Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
        item.setMillion2(million2Count);
        resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

        Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
        item.setMillion1(million1Count);
        resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

        Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
        item.setThousand(thousandCount);
        resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

        Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
        item.setHundred(hundredCount);
        resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

        Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
        item.setTen(tenCount);
        resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

        Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
        item.setYuan(yuanCount);
        resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

        Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
        item.setJiao(jiaoCount);
        resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

        Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).intValue();
        item.setFen(fenCount);


        //百元
        if (item.getHundredyuanCount() != null && item.getHundredyuanCount() > 0) {
            BigDecimal p1 = BigDecimal.valueOf(100 * item.getHundredyuanCount());

            Integer hundredyuanthousand3Count = p1.divide(BigDecimal.valueOf(100000)).intValue();
            item.setHundredyuanthousand3(hundredyuanthousand3Count);
            p1 = p1.subtract(BigDecimal.valueOf(hundredyuanthousand3Count * 100000));

            Integer hundredyuanthousand2Count = p1.divide(BigDecimal.valueOf(10000)).intValue();
            item.setHundredyuanthousand2(hundredyuanthousand2Count);
            p1 = p1.subtract(BigDecimal.valueOf(hundredyuanthousand2Count * 10000));

            Integer hundredyuanthousand1Count = p1.divide(BigDecimal.valueOf(1000)).intValue();
            item.setHundredyuanthousand1(hundredyuanthousand1Count);
            p1 = p1.subtract(BigDecimal.valueOf(hundredyuanthousand1Count * 1000));

            Integer hundredyuanhundredCount = p1.divide(BigDecimal.valueOf(100)).intValue();
            item.setHundredyuanhundred(hundredyuanhundredCount);

            item.setHundredyuanten(0);
            item.setHundredyuanyuan(0);
            item.setHundredyuanjiao(0);
            item.setHundredyuanfen(0);
        }

        //50元
        if (item.getFiftyyuanCount() != null && item.getFiftyyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(50 * item.getFiftyyuanCount());

            Integer fiftyyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setFiftyyuanthousand3(fiftyyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiftyyuanthousand3Count * 100000));

            Integer fiftyyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setFiftyyuanthousand2(fiftyyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiftyyuanthousand2Count * 10000));

            Integer fiftyyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setFiftyyuanthousand1(fiftyyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiftyyuanthousand1Count * 1000));

            Integer fiftyyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setFiftyyuanhundred(fiftyyuanhundredCount);

            item.setFiftyyuanten(0);
            item.setFiftyyuanyuan(0);
            item.setFiftyyuanjiao(0);
            item.setFiftyyuanfen(0);
        }

        //20元
        if (item.getTwentyyuanCount() != null && item.getTwentyyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(20 * item.getTwentyyuanCount());

            Integer twentyyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setTwentyyuanthousand3(twentyyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(twentyyuanthousand3Count * 100000));

            Integer twentyyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setTwentyyuanthousand2(twentyyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(twentyyuanthousand2Count * 10000));

            Integer twentyyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setTwentyyuanthousand1(twentyyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(twentyyuanthousand1Count * 1000));

            Integer twentyyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setTwentyyuanhundred(twentyyuanhundredCount);

            item.setTwentyyuanten(0);
            item.setTwentyyuanyuan(0);
            item.setTwentyyuanjiao(0);
            item.setTwentyyuanfen(0);
        }


        //10元
        if (item.getTenyuanCount() != null && item.getTenyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(10 * item.getTenyuanCount());

            Integer tenyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setTenyuanthousand3(tenyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(tenyuanthousand3Count * 100000));

            Integer tenyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setTenyuanthousand2(tenyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(tenyuanthousand2Count * 10000));

            Integer tenyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setTenyuanthousand1(tenyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(tenyuanthousand1Count * 1000));

            Integer tenyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setTenyuanhundred(tenyuanhundredCount);
            p2 = p2.subtract(BigDecimal.valueOf(tenyuanhundredCount * 100));

            Integer tenyuantenCount = p2.divide(BigDecimal.valueOf(10)).intValue();
            item.setTenyuanten(tenyuantenCount);

            item.setTenyuanyuan(0);
            item.setTenyuanjiao(0);
            item.setTenyuanfen(0);
        }

        //5元
        if (item.getFiveyuanCount() != null && item.getFiveyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(5 * item.getFiveyuanCount());

            Integer fiveyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setFiveyuanthousand3(fiveyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiveyuanthousand3Count * 100000));

            Integer fiveyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setFiveyuanthousand2(fiveyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiveyuanthousand2Count * 10000));

            Integer fiveyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setFiveyuanthousand1(fiveyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(fiveyuanthousand1Count * 1000));

            Integer fiveyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setFiveyuanhundred(fiveyuanhundredCount);
            p2 = p2.subtract(BigDecimal.valueOf(fiveyuanhundredCount * 100));

            Integer fiveyuantenCount = p2.divide(BigDecimal.valueOf(10)).intValue();
            item.setFiveyuanten(fiveyuantenCount);
            p2 = p2.subtract(BigDecimal.valueOf(fiveyuantenCount * 10));

            Integer fiveyuanyuanCount = p2.divide(BigDecimal.valueOf(1)).intValue();
            item.setFiveyuanyuan(fiveyuanyuanCount);

            item.setFiveyuanjiao(0);
            item.setFiveyuanfen(0);
        }

        //2元
        if (item.getTwoyuanCount() != null && item.getTwoyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(2 * item.getTwoyuanCount());

            Integer twoyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setTwoyuanthousand3(twoyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(twoyuanthousand3Count * 100000));

            Integer twoyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setTwoyuanthousand2(twoyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(twoyuanthousand2Count * 10000));

            Integer twoyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setTwoyuanthousand1(twoyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(twoyuanthousand1Count * 1000));

            Integer twoyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setTwoyuanhundred(twoyuanhundredCount);
            p2 = p2.subtract(BigDecimal.valueOf(twoyuanhundredCount * 100));

            Integer twoyuantenCount = p2.divide(BigDecimal.valueOf(10)).intValue();
            item.setTwoyuanten(twoyuantenCount);
            p2 = p2.subtract(BigDecimal.valueOf(twoyuantenCount * 10));

            Integer twoyuanyuanCount = p2.divide(BigDecimal.valueOf(1)).intValue();
            item.setTwoyuanyuan(twoyuanyuanCount);

            item.setTwoyuanjiao(0);
            item.setTwoyuanfen(0);
        }

        //1元
        if (item.getOneyuanCount() != null && item.getOneyuanCount() > 0) {
            BigDecimal p2 = BigDecimal.valueOf(1 * item.getOneyuanCount());

            Integer oneyuanthousand3Count = p2.divide(BigDecimal.valueOf(100000)).intValue();
            item.setOneyuanthousand3(oneyuanthousand3Count);
            p2 = p2.subtract(BigDecimal.valueOf(oneyuanthousand3Count * 100000));

            Integer oneyuanthousand2Count = p2.divide(BigDecimal.valueOf(10000)).intValue();
            item.setOneyuanthousand2(oneyuanthousand2Count);
            p2 = p2.subtract(BigDecimal.valueOf(oneyuanthousand2Count * 10000));

            Integer oneyuanthousand1Count = p2.divide(BigDecimal.valueOf(1000)).intValue();
            item.setOneyuanthousand1(oneyuanthousand1Count);
            p2 = p2.subtract(BigDecimal.valueOf(oneyuanthousand1Count * 1000));

            Integer oneyuanhundredCount = p2.divide(BigDecimal.valueOf(100)).intValue();
            item.setOneyuanhundred(oneyuanhundredCount);
            p2 = p2.subtract(BigDecimal.valueOf(oneyuanhundredCount * 100));

            Integer oneyuantenCount = p2.divide(BigDecimal.valueOf(10)).intValue();
            item.setOneyuanten(oneyuantenCount);
            p2 = p2.subtract(BigDecimal.valueOf(oneyuantenCount * 10));

            Integer oneyuanyuanCount = p2.divide(BigDecimal.valueOf(1)).intValue();
            item.setOneyuanyuan(oneyuanyuanCount);

            item.setOneyuanjiao(0);
            item.setOneyuanfen(0);
        }

        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillGhxjckpt> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillGhxjckpt> list = billGhxjckptDao.findAll(params);
        PageInfo<BillGhxjckpt> pageInfo = new PageInfo(list);

        return PageResult.<BillGhxjckpt>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillGhxjckpt> findAll(Map<String, Object> params) {
        return billGhxjckptDao.findAll(params);
    }

}
